using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Collections.Generic;

using VIT.Library.Web;
using VIT.DataHelper.DataAccess;

namespace VIT.Web.UI
{
    abstract public class VITModule : System.Web.UI.UserControl, IWebUI
    {
        private string TemplatePath
        {
            get
            {
                string curentPath = URL.getCurrentPath();
                bool isAdminPath = curentPath.ToLower().Contains("admin");
                if (isAdminPath) return Convert.ToString(HttpContext.Current.Application["TemplatePathAdmin"]);
                return Convert.ToString(HttpContext.Current.Application["TemplatePath"]);
            }
            set
            {
                string curentPath = URL.getCurrentPath();
                bool isAdminPath = curentPath.ToLower().Contains("admin");
                if (isAdminPath) HttpContext.Current.Application["TemplatePathAdmin"] = value;
                else HttpContext.Current.Application["TemplatePath"] = value;
            }
        }

        // giao dien
        public string Title { get; set; }
        public string Content { get; set; }
        public dynamic Params { get; set; }

        // URL
        public string URLCurrentPath { get; set; }
        public string URLCurrentPathAndQuery { get; set; }
        public string URLRoot { get; set; }
        public string URLBaseDir { get; set; }
        public string URLTemplatePath { get; set; }

        public VITModule()
        {
            URLCurrentPath = URL.getCurrentPath();
            URLCurrentPathAndQuery = URL.getPathAndQuery();
            URLRoot = URL.baseUrl;
            URLBaseDir = URL.baseDir;

            this.URLTemplatePath = this.TemplatePath;
        }

        public string getContent()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            this.RenderControl(hw);
            return sb.ToString();
        }

        #region inherit interface
        public string linkComponent(string ComponentName, string View = "", string Param = "")
        {
            return URL.linkComponent(ComponentName, View, Param);
        }
        public void RedirectComponent(string ComponentName, string View = "", string Param = "")
        {
            Response.Redirect(URL.linkComponent(ComponentName, View, Param));
        }
        public void TransferComponent(string ComponentName, string View = "", string Param = "")
        {
            Server.Transfer(URL.linkComponent(ComponentName, View, Param));
        }
        public VITContext VITData
        {
            get
            {
                if (Application["VITDataContext"] as VITContext == null)
                {
                    for (int i = 0; i < System.Configuration.ConfigurationManager.ConnectionStrings.Count; i++)
                    {
                        if (SelectConnectionString(System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString))
                        {
                            var strConn = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                            Application["VITDataContext"] = new VITContext(strConn);
                            break;
                        }
                    }
                }
                return Application["VITDataContext"] as VITContext;
            }
        }
        #endregion

        private static bool SelectConnectionString(string ConnectionString)
        {
            System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(ConnectionString);
            try
            {
                sqlConn.Open();
                sqlConn.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
